package cn.iocoder.yudao.module.member.service.usercreativetext;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.member.controller.admin.usercreativetext.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.usercreativetext.UserCreativeTextDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.member.dal.mysql.usercreativetext.UserCreativeTextMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.*;

/**
 * 用户创意文案 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class UserCreativeTextServiceImpl implements UserCreativeTextService {

    @Resource
    private UserCreativeTextMapper userCreativeTextMapper;

    @Override
    public Long createUserCreativeText(UserCreativeTextSaveReqVO createReqVO) {
        // 插入
        UserCreativeTextDO userCreativeText = BeanUtils.toBean(createReqVO, UserCreativeTextDO.class);
        userCreativeTextMapper.insert(userCreativeText);
        // 返回
        return userCreativeText.getId();
    }

    @Override
    public void updateUserCreativeText(UserCreativeTextSaveReqVO updateReqVO) {
        // 校验存在
        validateUserCreativeTextExists(updateReqVO.getId());
        // 更新
        UserCreativeTextDO updateObj = BeanUtils.toBean(updateReqVO, UserCreativeTextDO.class);
        userCreativeTextMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserCreativeText(Long id) {
        // 校验存在
        validateUserCreativeTextExists(id);
        // 删除
        userCreativeTextMapper.deleteById(id);
    }

    private void validateUserCreativeTextExists(Long id) {
        if (userCreativeTextMapper.selectById(id) == null) {
            throw exception(USER_CREATIVE_TEXT_NOT_EXISTS);
        }
    }

    @Override
    public UserCreativeTextDO getUserCreativeText(Long id) {
        return userCreativeTextMapper.selectById(id);
    }

    @Override
    public PageResult<UserCreativeTextDO> getUserCreativeTextPage(UserCreativeTextPageReqVO pageReqVO) {
        return userCreativeTextMapper.selectPage(pageReqVO);
    }

}